#include <QtGui>

#include "gudaywellwidget.h"

GudaywellWidget::GudaywellWidget(QWidget *parent)
	:QTableView(parent)
{
	model = new QSqlRelationalTableModel;
	initializeModel(model);
	setModel(model);
	
	verticalHeader()->hide();
	setEditTriggers(QAbstractItemView::AllEditTriggers);
	setAlternatingRowColors(true);
	setColumnHidden(0,true);
    setItemDelegate(new QSqlRelationalDelegate(this));
    resizeRowsToContents();
    
    setColumnWidth( 1, 120 );
    setColumnWidth( 2, 70 );
    setColumnWidth( 3, 70 );
    setColumnWidth( 4, 70 );
    setColumnWidth( 5, 120 );
}

void GudaywellWidget::initializeModel(QSqlRelationalTableModel *model)
{
	 model->setTable("gadwl");

     model->setEditStrategy(QSqlTableModel::OnManualSubmit);
     model->setRelation(1, QSqlRelation("subjects", "id", "subject"));
     model->setRelation(2, QSqlRelation("times", "id", "time"));
     model->setRelation(3, QSqlRelation("times", "id", "time"));
     model->setRelation(4, QSqlRelation("places", "id", "place"));
     model->setRelation(5, QSqlRelation("teachers", "id", "teacher"));

     model->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
     model->setHeaderData(1, Qt::Horizontal, QObject::tr("Subject"));
     model->setHeaderData(2, Qt::Horizontal, QObject::tr("From"));
     model->setHeaderData(3, Qt::Horizontal, QObject::tr("To"));
     model->setHeaderData(4, Qt::Horizontal, QObject::tr("Place"));
     model->setHeaderData(5, Qt::Horizontal, QObject::tr("Teacher"));

     model->select();
}

void GudaywellWidget::addRow()
{
	//int row = currentIndex().row() + 1;
	int row = model->rowCount();
	model->insertRows(row,1);
	resizeRowsToContents();
	
	QModelIndex index = model->index(row,1);
	setCurrentIndex(index);
	edit(index);
}

void GudaywellWidget::delRow()
{
	model->removeRows(currentIndex().row(),1);
}
